package com.googlecode.vmock.utils;

import java.util.Collection;
import java.util.Iterator;

/**
 * Utility class that assists the programmer in method arguments validation.
 * 
 * @author marcin.jagus
 */
public class Validate {

    /**
     * Protected constructors allows for subclassing.
     */
    protected Validate() {
    }

    /**
     * Checks if a given expression is true. Throws {@link IllegalArgumentException} otherwise.
     * 
     * @param expression Given expression
     */
    public static void isTrue(boolean expression) {
        isTrue(expression, "The validated expression is fale");
    }

    /**
     * Checks if a given expression is true. Throws {@link IllegalArgumentException} otherwise.
     * <p>
     * The exception is created with a given message.
     * 
     * @param expression Given expression
     * @param message Given message
     */
    public static void isTrue(boolean expression, String message) {
        if (!expression) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * Checks if a given object is not null. Throws {@link IllegalArgumentException} otherwise.
     * 
     * @param value Given object
     */
    public static void notNull(Object value) {
        notNull(value, "The validated object is null");
    }

    /**
     * Checks if a given object is not null. Throws {@link IllegalArgumentException} otherwise.
     * <p>
     * The exception is created with a given message.
     * 
     * @param value Given object
     * @param message Given message
     */
    public static void notNull(Object value, String message) {
        isTrue(value != null, message);
    }

    /**
     * Checks a given collection to make sure it does not contain any <code>null</code> elements. Throws {@link IllegalArgumentException} if a given
     * collection does not satisfy this condition.
     * 
     * @param collection Given collection
     */
    public static void noNullElements(Collection<?> collection) {
        noNullElements(collection, "The validated collection contains null element at index: %d");
    }

    /**
     * Checks a given collection to make sure it does not contain any <code>null</code> elements. Throws {@link IllegalArgumentException} if a given
     * collection does not satisfy this condition. Exception is created with a given message.
     * 
     * @param collection Given collection
     * @param message Given exception message
     */
    public static void noNullElements(Collection<?> collection, String message) {
        notNull(collection);

        int i = 0;
        for (Iterator<?> it = collection.iterator(); it.hasNext(); i++) {
            if (it.next() == null) {
                throw new IllegalArgumentException(String.format(message, i));
            }
        }
    }

    /**
     * Checks a given array to make sure it does not contain any <code>null</code> elements. Throws {@link IllegalArgumentException} if a given array
     * does not satisfy this condition.
     * 
     * @param array Given array
     */
    public static void noNullElements(Object[] array) {
        noNullElements(array, "The validated array contains null element at index: %d");
    }

    /**
     * Checks a given array to make sure it does not contain any <code>null</code> elements. Throws {@link IllegalArgumentException} if a given array
     * does not satisfy this condition. Exception is created with a given message.
     * 
     * @param array Given array
     * @param message Given exception message
     */
    public static void noNullElements(Object[] array, String message) {
        notNull(array);

        for (int i = 0; i < array.length; i++) {
            if (array[i] == null) {
                throw new IllegalArgumentException(String.format(message, i));
            }
        }
    }
}
